package com.bistu.openlab.uofspreadsheet.xmlparse;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.bistu.openlab.uofspreadsheet.model.ExcelSheet;
import com.bistu.openlab.uofspreadsheet.util.XmlDomHelper;

/*
 *@author xiaoguang.gao@oracle.com
 *@version 2013-5-9 
 */
public class ExcelSheetParse implements IXmlParse<ExcelSheet>{
	private static ExcelSheetParse parse = new ExcelSheetParse();
	private ExcelSheetParse(){}
	/**
	 * 
	 * @return
	 */
	public static ExcelSheetParse getInstance(){
		return parse;
	}

	@Override
	public ExcelSheet readFromElement(Element root) {
		ExcelSheet sheet = null;
		if(root!=null&&root.getName().equals("工作表_E825")){		
			sheet = new ExcelSheet();
			//标识符_E7AC="SHEET_1" 名称_E822="工作表1" 式样引用_E824="DEFSTYLE
			sheet.setIdentifier(XmlDomHelper.GetAttributeValue(root, "标识符_E7AC"));
			sheet.setName(XmlDomHelper.GetAttributeValue(root, "名称_E822"));
			sheet.setStyleReference(XmlDomHelper.GetAttributeValue(root, "式样引用_E824"));
			//attribute
			ExcelSheetAttParse attParse = ExcelSheetAttParse.getInstance();
			sheet.setSheetAtt(attParse.readFromElement((Element)root.selectSingleNode("表:工作表属性_E80D")));
			//content
			ExcelSheetContentParse contentParse = ExcelSheetContentParse.getInstance();
			sheet.setSheetContent(contentParse.readFromElement((Element)root.selectSingleNode("表:工作表内容_E80E")));			
		}
		return sheet;
	}

	@Override
	public Element writeToElement(ExcelSheet obj) {
		Element root = null;
		if(obj!=null){
			root = DocumentHelper.createElement("表:工作表_E825");
			root.addAttribute("标识符_E7AC", obj.getIdentifier());
			root.addAttribute("名称_E822", obj.getName());
			root.addAttribute("式样引用_E824",obj.getStyleReference());
			//attribute
			ExcelSheetAttParse attParse = ExcelSheetAttParse.getInstance();
			root.add(attParse.writeToElement(obj.getSheetAtt()));
			//content
			ExcelSheetContentParse contentParse = ExcelSheetContentParse.getInstance();
			root.add(contentParse.writeToElement(obj.getSheetContent()));
		}
		return root;
	}

}
